package cn.bytepulse.web.admin.service.impl;

import cn.bytepulse.pojo.AjaxResult;
import cn.bytepulse.utils.PageUtils;
import cn.bytepulse.web.admin.entity.SysMenu;
import cn.bytepulse.web.admin.entity.SysRole;
import cn.bytepulse.web.admin.entity.SysRoleMenu;
import cn.bytepulse.web.admin.entity.SysUserRole;
import cn.bytepulse.web.admin.mapper.SysMenuMapper;
import cn.bytepulse.web.admin.mapper.SysRoleMapper;
import cn.bytepulse.web.admin.mapper.SysRoleMenuMapper;
import cn.bytepulse.web.admin.mapper.SysUserRoleMapper;
import cn.bytepulse.web.admin.service.SystemRoleService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import static cn.bytepulse.utils.ReqUtils.getUserId;

/**
 * @author jiejiebiezheyang
 * @since 2024-07-09 22:00
 */
@Service
@Transactional
@RequiredArgsConstructor
public class SystemRoleServiceImpl implements SystemRoleService {

    private final SysRoleMenuMapper sysRoleMenuMapper;

    private final SysRoleMapper sysRoleMapper;

    private final SysMenuMapper sysMenuMapper;

    private final SysUserRoleMapper sysUserRoleMapper;

    /**
     * 角色列表
     */
    @Override
    public AjaxResult list() {
        PageUtils.startPage();
        QueryWrapper<SysRole> wrapper = new QueryWrapper<>();
        List<SysRole> roles = sysRoleMapper.selectList(wrapper);
        PageInfo<SysRole> pageInfo = PageInfo.of(roles);
        return AjaxResult.success(pageInfo);
    }

    /**
     * 添加角色
     */
    @Override
    public AjaxResult add(SysRole sysRole) {
        SysRole one = new SysRole();
        one.setRoleName(sysRole.getRoleName());
        one.setRoleCode(sysRole.getRoleCode());
        one.setStatus(sysRole.getStatus());
        one.setCreateBy(getUserId());
        one.setCreateTime(new Date());
        one.setUpdateBy(getUserId());
        one.setUpdateTime(new Date());
        sysRoleMapper.insert(one);
        return AjaxResult.success();
    }

    /**
     * 更新角色
     */
    @Override
    public AjaxResult update(SysRole sysRole) {
        SysRole one = new SysRole();
        one.setId(sysRole.getId());
        one.setRoleName(sysRole.getRoleName());
        one.setRoleCode(sysRole.getRoleCode());
        one.setStatus(sysRole.getStatus());
        sysRoleMapper.updateById(one);
        return AjaxResult.success();
    }

    /**
     * 删除角色
     */
    @Override
    public AjaxResult delete(Integer id) {
        QueryWrapper<SysUserRole> w1 = new QueryWrapper<>();
        w1.eq("role_id", id);
        sysUserRoleMapper.delete(w1);
        QueryWrapper<SysRoleMenu> w2 = new QueryWrapper<>();
        w2.eq("role_id", id);
        sysRoleMenuMapper.delete(w2);
        sysRoleMapper.deleteById(id);
        return AjaxResult.success();
    }

    /**
     * 获取关联菜单
     */
    @Override
    public AjaxResult getRelation(Integer id) {
        // 查询关联菜单id
        List<Integer> menuIds = sysRoleMenuMapper.selectMenuIdByRoleId(id);
        return AjaxResult.success(menuIds);
    }

    /**
     * 获取菜单简要
     */
    @Override
    public AjaxResult getMenuConcise() {
        // 查询菜单
        List<SysMenu> menus = sysMenuMapper.selectMenuConcise();
        // 获取权限
        List<SysMenu> perms = menus.stream().filter(m -> "B".equalsIgnoreCase(m.getMenuType())).toList();
        // 获取二级
        List<SysMenu> seconds = menus.stream()
                .filter(m -> "M".equalsIgnoreCase(m.getMenuType()) && m.getParentId() != 0)
                .toList();
        for (SysMenu second : seconds) {
            List<SysMenu> c = perms.stream().filter(m -> m.getParentId().equals(second.getId())).toList();
            second.setChildren(c);
        }
        // 获取一级
        List<SysMenu> firsts = menus.stream().filter(m -> m.getParentId() == 0).toList();
        for (SysMenu first : firsts) {
            List<SysMenu> c = seconds.stream().filter(m -> m.getParentId().equals(first.getId())).toList();
            first.setChildren(c);
        }
        return AjaxResult.success(firsts);
    }

    /**
     * 更新角色菜单关联
     */
    @Override
    public AjaxResult updateRelation(Integer roleId, Integer[] menuIds) {
        QueryWrapper<SysRoleMenu> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id", roleId);
        sysRoleMenuMapper.delete(wrapper);
        if (menuIds != null && menuIds.length > 0) {
            List<Integer> integers = Arrays.asList(menuIds);
            sysRoleMenuMapper.insertBatchByRoleId(roleId, integers);
        }
        return AjaxResult.success();
    }
}
